package com.wuliupai.delivery.wuliupaidelivery.adapter;

import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.wuliupai.delivery.wuliupaidelivery.R;
import com.wuliupai.delivery.wuliupaidelivery.bean.OrderBean;
import com.wuliupai.delivery.wuliupaidelivery.utils.DateUtils_;
import com.wuliupai.delivery.wuliupaidelivery.utils.StringUtils;

import java.util.List;

public class OrderListAdapter extends RecyclerView.Adapter<OrderListAdapter.VHolder> {
    private Context context;
    private List<OrderBean> dataList;
    private DateUtils_ dateUtils;

    private OnAdapterClickListener mOnAdapterClickListener;

    public OrderListAdapter(Context context, List<OrderBean> dataList) {
        this.context = context;
        this.dataList = dataList;
        dateUtils = DateUtils_.getInstance_(context);
    }

    public void setOnAdapterClickListener(OnAdapterClickListener mOnAdapterClickListener) {
        this.mOnAdapterClickListener = mOnAdapterClickListener;
    }


    @Override
    public VHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_order_list_item_list, parent, false);
        return new OrderListAdapter.VHolder(view);
    }

    @Override
    public void onBindViewHolder(VHolder holder, int position) {

        final int pos = holder.getLayoutPosition();

        try {
            OrderBean bean = dataList.get(pos);

            holder.tv_orderNo.setText(bean.getOrderCode());
            holder.tv_fromAddress.setText(bean.getStartAddress());
            holder.tv_toAddress.setText(bean.getEndAddress());

            StringBuilder sb = new StringBuilder();
            sb.append(bean.getGoodsName())
                    .append(" / ")
                    .append(StringUtils.formatWeight(bean.getGoodsWeight()))
                    .append(" / ")
                    .append(StringUtils.formatVolume(bean.getGoodsVolume()))
                    .append(" / ")
                    .append(bean.getGoodsCount())
                    .append("件");
            holder.tv_goodsName.setText(sb.toString());

            holder.tv_orderTime.setText(dateUtils.formatData(bean.getRefreshTime(), "yy-M-d HH:mm"));

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (null != mOnAdapterClickListener) {
            holder.cl_item.setOnClickListener(v -> mOnAdapterClickListener.onItemClick(pos));
            holder.cl_item.setOnLongClickListener(v -> {
                mOnAdapterClickListener.onItemLongClick(pos);
                return true;
            });
            holder.btn_grab.setOnClickListener(v -> mOnAdapterClickListener.onGrabBtnClick(pos));
        }

    }

    @Override
    public int getItemCount() {
        return null == dataList ? 0 : dataList.size();
    }

    static class VHolder extends RecyclerView.ViewHolder {
        private ConstraintLayout cl_item; // item
        private TextView tv_orderNo; // 订单编号
        private TextView tv_fromAddress; // 始发地
        private TextView tv_toAddress; // 目的地
        private TextView tv_goodsName; // 货物信息

//        private TextView tv_needToPayLabel; // 需付的label
//        private TextView tv_needToPay; // 需付
        private TextView tv_orderTime; // 时间

        private Button btn_grab; // 抢单按钮

        VHolder(View itemView) {
            super(itemView);
            cl_item = (ConstraintLayout) itemView.findViewById(R.id.cl_item);
            tv_orderNo = (TextView) itemView.findViewById(R.id.tv_orderNo);
            tv_fromAddress = (TextView) itemView.findViewById(R.id.tv_fromAddress);
            tv_toAddress = (TextView) itemView.findViewById(R.id.tv_toAddress);
            tv_goodsName = (TextView) itemView.findViewById(R.id.tv_goodsName);
//            tv_needToPayLabel = (TextView) itemView.findViewById(R.id.tv_needToPayLabel);
//            tv_needToPay = (TextView) itemView.findViewById(R.id.tv_needToPay);
            tv_orderTime = (TextView) itemView.findViewById(R.id.tv_orderTime);
            btn_grab = (Button) itemView.findViewById(R.id.btn_grab);
        }
    }

    public interface OnAdapterClickListener {
        void onItemClick(int pos);

        void onItemLongClick(int pos);

        void onGrabBtnClick(int pos);
    }
}